package org.custom.spring.boot.security.application;

import org.custom.spring.boot.security.entity.security.SecurityProperties;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static org.custom.spring.boot.security.constant.SecurityConstant.*;

/**
 * Spring-Boot 项目启动后初始化程序
 *
 * @author LiKun
 */
@Component
public class ApplicationStart implements ApplicationRunner {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private SecurityProperties securityProperties;

    /**
     * 更具配置信息清空违反规则的用户 Token 信息
     *
     * @param args 相关参数
     */
    @Override
    public void run(ApplicationArguments args) {
        final String tokenKey = securityProperties.isAllowRepeatLogin() ? TOKEN_KEY_ID : TOKEN_KEY_SNOW;
        redisTemplate.delete(redisTemplate.keys(tokenKey + PATTERN_ANY));
    }

}
